// AssessmentControllerV2.java
package com.university.mentalhealthplatform.teacherV2.controller;

import com.university.mentalhealthplatform.teacherV2.dto.AssessmentDTO;
import com.university.mentalhealthplatform.teacherV2.service.AssessmentServiceV2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/v2/assessments")
@CrossOrigin(origins = "http://localhost:3000") // Adjust as needed
public class AssessmentControllerV2 {
    @Autowired
    private AssessmentServiceV2 assessmentServiceV2;

    @GetMapping
    public ResponseEntity<List<AssessmentDTO>> getAllAssessments() {
        List<AssessmentDTO> assessments = assessmentServiceV2.findAllWithStudent()
                .stream()
                .map(AssessmentDTO::convertToDTO)
                .collect(Collectors.toList());
        return ResponseEntity.ok(assessments);
    }

    @GetMapping("/{id}")
    public ResponseEntity<AssessmentDTO> getAssessmentById(@PathVariable Long id) {
        return assessmentServiceV2.getAssessmentById(id)
                .map(AssessmentDTO::convertToDTO)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @GetMapping("/search")
    public ResponseEntity<List<AssessmentDTO>> searchAssessments(@RequestParam String keyword) {
        List<AssessmentDTO> assessments = assessmentServiceV2.searchAssessments(keyword)
                .stream()
                .map(AssessmentDTO::convertToDTO)
                .collect(Collectors.toList());
        return ResponseEntity.ok(assessments);
    }

    // 新增端点：获取统计数据
    @GetMapping("/statistics")
    public ResponseEntity<Map<String, Object>> getStatistics() {
        Map<String, Object> statistics = assessmentServiceV2.getStatistics();
        return ResponseEntity.ok(statistics);
    }
}